package com.example.wac.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.example.wac.service.CommonService;
import com.example.wac.vo.ApiResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;

/**
 * @author wuancheng
 * @description
 * @date 2022-01-19 17:47:00
 */

@Service
public class CommonServiceImpl implements CommonService {

    @Override
    public ApiResult upLoadLocalFile(MultipartFile file) {
        InputStream inputStream = null;
        FileOutputStream outputStream=null;
        try {
            inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            outputStream = new FileOutputStream(new File("D:\\FileTest\\"+fileName));
            byte bytes[]=new byte[1024];
            while (inputStream.read(bytes)!=-1){
                outputStream.write(bytes,0,bytes.length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ApiResult.success();
    }

    @Override
    public ApiResult upLoadLocalFileTransfer(MultipartFile file) {
        try {
            String fileName = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + file.getOriginalFilename();
            String filePath = "D:\\FileTest\\";
            // 文件直接复制到新建目录下的文件
            file.transferTo(new File(filePath, fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ApiResult.success();
    }

    @Override
    public void downLoadLocalFile(HttpServletResponse response, String fileName) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            // 设置response响应文件会进行下载，不设置则直接显示文件内容
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("导出" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN), "UTF-8") + fileName);
            inputStream = new FileInputStream(new File("D://FileTest//" + fileName));
            outputStream = response.getOutputStream();
            int len = inputStream.available();
            byte[] bytes =new byte[len];
            while (inputStream.read(bytes)!=-1){
                outputStream.write(bytes,0,bytes.length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
